package com.hangwei.paperless.fragment.sub;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangwei.paperless.BottomPopType;
import com.hangwei.paperless.R;
import com.hangwei.paperless.SubRepairActivity;
import com.hangwei.paperless.adapter.sub.SubDataAdapter;
import com.hangwei.paperless.model.bean.CardToSeal;
import com.hangwei.paperless.model.bean.DataFilling;
import com.hangwei.paperless.presenter.StaticPresenter;
import com.hangwei.paperless.utils.NFCCallBack;
import com.hangwei.paperless.widget.MainBottomPopupWindow;
import com.twelve.all.base.LoadFragment;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 张龙臣 on 2020/11/27.
 * 数据填报
 */

public class SubDataFragment extends LoadFragment {

    private SubDataAdapter mAdapter;
    private String mBillId;
    private SubRepairActivity mActivity;
    private String mUserId;
    private MainBottomPopupWindow mMainBottomPopupWindow;

    @Override
    protected View onRealCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mBillId = arguments.getString("billId");
            mActivity = (SubRepairActivity) getActivity();
        }
        final View inflate = inflater.inflate(R.layout.fragment_sub_data, container, false);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new SubDataAdapter(null) {
            @Override
            protected void onItemDoubleClick(DataFilling item) {
                mActivity.onNotLotDoubleClick(item);
            }

            @Override
            protected void seal(final DataFilling item) {
//                mActivity.showBottomPopupWindow(BottomPopType.NA)
//                View view = LayoutInflater.from(mActivity).inflate(R.layout.popwindow_na, null);
                mMainBottomPopupWindow = mActivity.showBottomPopupWindow(BottomPopType.NA,item.id);
                //覆盖设置
                mMainBottomPopupWindow.setOnNAListener(new MainBottomPopupWindow.OnNAListener() {
                    @Override
                    public void onNA(String stampId, String naTypeId) {
                        mActivity.sealByCard("", new NFCCallBack() {
                            @Override
                            public void onCallBack(List<CardToSeal.StampBean> stampIds, String userId) {
                                if (stampIds == null || stampIds.size() == 0) {
                                    ToastUtil.showCenterToast("无印章！");
                                } else {
                                    final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
                                    loadingDialog.show();
                                    StaticPresenter.stampNASubBills(mUserId, mActivity.getBillId(), item.id, userId, stampIds.get(0).stampId, naTypeId, new CallBack<Object>() {
                                        @Override
                                        public void onCompleted(Object o) {
                                            loadingDialog.dismiss();
                                            mMainBottomPopupWindow.dismiss();
                                            ToastUtil.showCenterToast("NA成功！");
                                            refresh();
                                        }

                                        @Override
                                        public void onError() {
                                            loadingDialog.dismiss();
                                            ToastUtil.showCenterToast("NA失败！");
                                        }
                                    });
                                }
                            }
                        });

                    }
                });
                mMainBottomPopupWindow.showAtLocation(inflate, Gravity.BOTTOM, 0, 0);
            }

            @Override
            protected void showBigPic(ArrayList<String> picList, int position) {
                mActivity.showBigPic(picList, position);
            }
        };
        recyclerView.setAdapter(mAdapter);
//        getRoutingSheetSubBills  添加图片查看界面
//        stampNASubBills
        final LoadingDialog loadingDialog = new LoadingDialog(mActivity);
        loadingDialog.show();
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
        StaticPresenter.getRoutingSheetSubBills(mUserId, mBillId, new CallBack<List<DataFilling>>() {
            @Override
            public void onCompleted(List<DataFilling> dataFillings) {
                loadingDialog.dismiss();
                mAdapter.setNewData(dataFillings);
            }

            @Override
            public void onError() {
                loadingDialog.dismiss();
            }
        });
        return inflate;
    }
}
